package com.owc.controller.admin;

import com.owc.common.custom.annotation.Advice;
import com.owc.common.enums.ExceptionEnum;
import com.owc.domain.BaseResult;
import com.owc.domain.dto.NoticeDTO;
import com.owc.domain.dto.admin.NoticeInfoDTO;
import com.owc.domain.vo.admin.NoticeVo;
import com.owc.domain.vo.admin.QueryVo;
import com.owc.service.admin.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author xiaom
 * @Date 2020/3/3 10:24
 * @Version 1.0.0
 * @Description <>
 **/
@RestController
@RequestMapping(value = "/admin/notice")
@Api(tags = "(manage-V2)公告管理")
public class NoticeController {

    private final NoticeService noticeService;

    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @PostMapping(value = "/add")
    @ApiOperation("新建产品公告")
    public BaseResult<Boolean> add(@RequestBody @Valid NoticeVo noticeVo) throws Exception {
        return BaseResult.success(noticeService.save(noticeVo.converterTo()));
    }

    @PostMapping(value = "/list")
    @ApiOperation("获取公告列表")
    public BaseResult<NoticeInfoDTO> list(@RequestBody QueryVo queryVo){
        return BaseResult.makePageBean(noticeService.pageList(queryVo));
    }

    @PostMapping(value = "/update")
    @ApiOperation("更新公告")
    public BaseResult<Boolean> update(@RequestBody NoticeInfoDTO noticeInfoDTO) throws Exception {
        Assert.notNull(noticeInfoDTO.getId(), ExceptionEnum.PARAMS_MISS_ERR.getCNMessage());
        return BaseResult.success(noticeService.update(noticeInfoDTO.converterTo()));
    }
}
